//
//  YWebUIViewController.swift
//  YFree
//
//  Created by YMac on 2018/12/17.
//  Copyright © 2018 yangzhe. All rights reserved.
//
import JavaScriptCore

@objc open class YWebUIViewController: YUIViewController {
    var webView:YWebView?
    @objc var js:YJSDelegate?
    
    var first = true
    
    open override func yOnCreate() {
        doInit()
    }
    
    open override func yViewWillAppear() {
        if YConfig.preYWebView {
            if !first { doInit() }
        }
    }
    
    open override func yViewWillDisappear() {
        if YConfig.preYWebView { unInit() }
    }
    
    open override func yOnDestory() {
        if !YConfig.preYWebView { unInit() }
    }
    
    func doInit(){
        js = getJSDelegate()
        for subView in self.view.subviews {
            if subView.tag == YConfig.yWebViewTag {
                var hasWebView = false
                subView.subviews.forEach { if $0 is YWebView { hasWebView = true } }
                if !hasWebView {
                    if YConfig.yWebView == nil {
                        self.webView = YWebView(frame: CGRect(x: 0, y: 0, width: subView.frame.width, height: subView.frame.height))
                    } else {
                        self.webView = YConfig.yWebView
                        self.webView?.isHidden = false
                        self.webView?.frame = CGRect(x: 0, y: 0, width: subView.frame.width, height: subView.frame.height)
                    }
                    self.webView?.configuration.preferences.setValue(true, forKey: "allowFileAccessFromFileURLs")
                    subView.addSubview(webView!)
                    webView!.scrollView.bounces = false
                    webView!.scrollView.bouncesZoom = false
                    webView!.navigationDelegate = self.js
                    webView!.addJavascriptObject(js, namespace: js!.getJsObjName())
                }
            }
        }
        if let htmlFile = Bundle.main.path(forResource: getWebName()!, ofType: "html") {
            if let url = URL(string: "\(URL(fileURLWithPath: htmlFile).absoluteString)\(getParams().addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)"){
                webView?.load(URLRequest(url: url))
                //                webView?.loadFileURL(url, allowingReadAccessTo: url.deletingLastPathComponent())
            }
        }
        first = false
    }
    
    func unInit() {
        webView?.stopLoading()
        webView?.navigationDelegate = nil
        webView?.loadHTMLString("", baseURL: nil)
        webView?.isHidden = true
        webView?.removeFromSuperview()
        js = nil
        webView = nil
    }
    
    open func getWebView() -> YWebView? { return webView }
    
    open func yEvaluateScript(_ script:String) {
        webView?.evaluateJavaScript(script, completionHandler: nil)
    }
    
    open func yEvaluateScript(method:String, argumengs:[Any]?, cocompletionHandlerm:((Any?) -> Void)?){
        webView?.callHandler(method, arguments: argumengs, completionHandler: cocompletionHandlerm)
    }
    
    open func getParams() -> String { return "" }
    open func getJSDelegate() -> YJSDelegate? { return YJSDelegate() }
    open func getWebName() -> String? { return nil }
}
